﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WucCloud.Utility
{
    /// <summary>
    /// HettpContext 拓展帮助类
    /// </summary>
    public static class HttpContextHelper
    {
        private static IHttpContextAccessor _accessor;
        public static HttpContext Current => _accessor.HttpContext;
        internal static void Configure(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        #region Session
        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetSession(string key, string value)
        {
            Current.Session.SetString(key, value);
        }

        /// <summary>
        /// 设置session
        /// </summary>
        /// <typeparam name="T">存储对象类型</typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetSession<T>(string key, T value)
        {
            if (value != null)
            {
                SetSession(key, JsonConvert.SerializeObject(value));
            }
        }

        /// <summary>
        /// 获取session
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSession(string key)
        {
            return Current.Session.GetString(key);
        }

        /// <summary>
        /// 获取session
        /// </summary>
        /// <typeparam name="T">存储对象类型</typeparam>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static T GetSession<T>(string key)
        {
            string v = GetSession(key);
            if (string.IsNullOrWhiteSpace(v))
            {
                return default(T);
            }
            else
            {
                return JsonConvert.DeserializeObject<T>(v);
            }
        }
        #endregion

        #region HttpContext拓展方法  需要在Startup中注入使用
        public static void AddHttpContextHelperAccessor(this IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

        public static IApplicationBuilder UseHttpContextHelper(this IApplicationBuilder app)
        {
            var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
            HttpContextHelper.Configure(httpContextAccessor);
            return app;
        }

        /// <summary>
        /// 获取验证消息提示并格式化提示
        /// </summary>
        public static string GetValidationSummary(this ModelStateDictionary modelState, string separator = "\r\n")
        {
            if (modelState.IsValid) return null;

            foreach (var item in modelState)
            {
                var state = item.Value;
                var message = state.Errors.FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.ErrorMessage))?.ErrorMessage;
                if (string.IsNullOrWhiteSpace(message))
                {
                    message = state.Errors.FirstOrDefault(o => o.Exception != null)?.Exception.Message;
                }
                if (string.IsNullOrWhiteSpace(message)) continue;
                return message;
            }
            return string.Empty;
        }
        #endregion
    }
}
